package us.sunrisemorning.mykancolle.kcsapi;

import java.util.List;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jfinal.plugin.activerecord.Db;

import us.sunrisemorning.mykancolle.config.GameData;
import us.sunrisemorning.mykancolle.model.Basic;
import us.sunrisemorning.mykancolle.model.Furniture;
import us.sunrisemorning.mykancolle.model.Kdock;
import us.sunrisemorning.mykancolle.model.Mapcell;
import us.sunrisemorning.mykancolle.model.Mapinfo;
import us.sunrisemorning.mykancolle.model.Material;
import us.sunrisemorning.mykancolle.model.Params;
import us.sunrisemorning.mykancolle.model.PresetDeck;
import us.sunrisemorning.mykancolle.model.Slotitem;
import us.sunrisemorning.mykancolle.model.Useitem;
import us.sunrisemorning.mykancolle.model.User;
import us.sunrisemorning.mykancolle.utils.ApiController;

public class ApiGetMemberController extends ApiController {
    public void require_info() {
        User u = getCurrentUser();

        JSONObject result = new JSONObject();
        result.put("basic", getBasic(u));
        result.put("slot_item", getSlotitem(u.getId()));
        result.put("unsetslot", getUnsetslot(u.getId()));
        result.put("kdock", getKdock(u.getId()));
        result.put("useitem", getUseitem(u.getId()));
        result.put("furniture", getFurniture(u.getId()));

        renderApiJson(result);
    }

    public void slot_item() {
        User u = getCurrentUser();

        JSONArray result = new JSONArray();
        List<Slotitem> items = getSlotitem(u.getId());
        result.addAll(items);

        renderApiJson(result);
    }

    public void unsetslot() {
        User u = getCurrentUser();

        renderApiJson(getUnsetslot(u.getId()));
    }

    public void useitem() {
        User u = getCurrentUser();

        JSONArray result = new JSONArray();
        List<Useitem> items = getUseitem(u.getId());
        result.addAll(items);

        renderApiJson(result);
    }

    public void payitem() {
        // TODO:查询未取出的氪金道具
        JSONArray result = new JSONArray();

        renderApiJson(result);
    }

    public void preset_deck() {
        User u = getCurrentUser();

        JSONObject result = new JSONObject();
        Params param = Params.dao.findById(u.getId());
        result.put("max_num", param.getMax_preset_deck());
        result.put("deck", getPresetDeck(u.getId()));

        renderApiJson(result);
    }

    public void kdock() {
        User u = getCurrentUser();

        JSONArray result = new JSONArray();
        result.addAll(getKdock(u.getId()));

        renderApiJson(result);
    }

    public void material() {
        User u = getCurrentUser();

        JSONArray result = new JSONArray();
        result.addAll(getMaterial(u.getId()));

        renderApiJson(result);
    }

    public void mapinfo() {
        User u = getCurrentUser();

        JSONArray result = new JSONArray();
        result.addAll(getMapinfo(u.getId()));

        renderApiJson(result);
    }

    public void mapcell() {
        User u = getCurrentUser();

        int maparea_id = getParaToInt("api_maparea_id");
        int mapinfo_no = getParaToInt("api_mapinfo_no");

        JSONArray result = new JSONArray();
        result.addAll(getMapcell(u.getId(), maparea_id, mapinfo_no));

        renderApiJson(result);
    }

    public void record() {
        User u = getCurrentUser();
        Basic basic = Basic.dao.findById(u.getId());

        JSONObject result = new JSONObject();
        result.put("member_id", basic.getMember_id());
        result.put("nickname", basic.getNickname());
        result.put("nickname_id", basic.getNickname_id());
        result.put("cmt", basic.getComment());
        result.put("cmt_id", basic.getComment_id());
        result.put("photo_url", "");
        result.put("level", basic.getLevel());
        result.put("rank", basic.getRank());
        result.put("experience", new int[] { basic.getExperience(), 0 });
        JSONObject war = new JSONObject();
        war.put("win", String.valueOf(basic.getSt_win()));
        war.put("lose", String.valueOf(basic.getSt_lose()));
        int total = basic.getSt_win() + basic.getSt_lose();
        int rate = total == 0 ? 0 : (basic.getSt_win() * 100 / total);
        war.put("rate", String.valueOf(rate / 100.0));
        result.put("war", war);
        JSONObject mission = new JSONObject();
        mission.put("count", String.valueOf(basic.getMs_count()));
        mission.put("success", String.valueOf(basic.getMs_success()));
        rate = basic.getMs_count() == 0 ? 0 : (basic.getMs_success() * 100 / basic.getMs_count());
        mission.put("rate", String.valueOf(rate / 100.0));
        result.put("mission", mission);
        JSONObject practice = new JSONObject();
        practice.put("win", String.valueOf(basic.getPt_win()));
        practice.put("lose", String.valueOf(basic.getPt_lose()));
        total = basic.getPt_win() + basic.getPt_lose();
        rate = total == 0 ? 0 : (basic.getPt_win() * 100 / total);
        practice.put("rate", String.valueOf(rate / 100.0));
        result.put("practice", practice);
        result.put("friend", 0);
        long deckCount = Db.queryLong("select count(id) from Deck where member_id=?", u.getId());
        result.put("deck", deckCount);
        long kdoc = Db.queryLong("select count(id) from Kdock where state>-1 and member_id=?", u.getId());
        result.put("kdoc", kdoc);
        long ndoc = Db.queryLong("select count(id) from Ndock where state>-1 and member_id=?", u.getId());
        result.put("ndoc", ndoc);
        long shipCount = Db.queryLong("select count(id) from Ship where user=?", u.getId());
        result.put("ship", new long[] { shipCount, basic.getMax_chara() });
        long slotitemCount = Db.queryLong("select count(id) from Slotitem where user=?", u.getId());
        result.put("slotitem", new long[] { slotitemCount, basic.getMax_slotitem() });
        long furniture_count = Db.queryLong("select count(id) from Furniture where user=?", u.getId());
        result.put("furniture", furniture_count);
        result.put("complate", new String[] { "0.0", "0.0" });
        result.put("large_dock", basic.getLarge_dock());
        result.put("material_max", (basic.getLevel() + 3) * 250);

        renderApiJson(result);
    }

    private JSONObject getPresetDeck(long userId) {
        List<PresetDeck> decks = PresetDeck.dao.find("select * from preset_deck where user=? and ship1<>-1 order by preset_no", userId);
        JSONObject result = new JSONObject();
        for (int i = 0; i < decks.size(); i++) {
            PresetDeck deck = decks.get(i);
            result.put(String.valueOf(deck.getPreset_no()), deck);
        }
        return result;
    }

    private JSONObject getBasic(User user) {
        JSONObject basic = new JSONObject();
        basic.put("member_id", user.getId());
        basic.put("firstflag", 1);
        return basic;
    }

    private List<Slotitem> getSlotitem(long userId) {
        List<Slotitem> slotitemList = Slotitem.dao.find("select id,slotitem_id,locked,level,alv from Slotitem where user=?", userId);
        return slotitemList;
    }

    private JSONObject getUnsetslot(long userId) {
        JSONObject result = new JSONObject();

        // TODO: 需要改为未装备的item的查询
        List<Slotitem> slotitemList = Slotitem.dao.find("select id,slotitem_id,locked,level,alv from Slotitem where user=?", userId);
        for (Slotitem item : slotitemList) {
            int category = GameData.getSlotitemData().get(item.getSlotitem_id()).getJSONArray("api_type").getInteger(2);
            JSONArray categoryItems = result.getJSONArray("slottype" + category);
            if (categoryItems == null) {
                categoryItems = new JSONArray();
                result.put("slottype" + category, categoryItems);
            }
            categoryItems.add(item.getId());
        }

        for (int category : GameData.getSlotitemEquiptypeData().keySet()) {
            if (!result.containsKey("slottype" + category)) {
                result.put("slottype" + category, -1);
            }
        }

        return result;
    }

    private List<Kdock> getKdock(long userId) {
        List<Kdock> kdockList = Kdock.dao.find("select id,state,created_ship_id,complete_time,complete_time_str,item1,item2,item3,item4,item5 from Kdock where member_id=?", userId);
        return kdockList;
    }

    private List<Useitem> getUseitem(long userId) {
        List<Useitem> useitemList = Useitem.dao.find("select id,count from Useitem where user=?", userId);
        return useitemList;
    }

    private List<Furniture> getFurniture(long userId) {
        List<Furniture> furnitureList = Furniture.dao.find("select id,furniture_type,furniture_no,furniture_id from Furniture where user=?", userId);
        return furnitureList;
    }

    public List<Material> getMaterial(long userId) {
        List<Material> result = Material.dao.find("select * from Material where member_id=?", userId);
        return result;
    }

    public List<Mapinfo> getMapinfo(long userId) {
        List<Mapinfo> result = Mapinfo.dao.find("select * from Mapinfo where member_id=? order by id", userId);
        return result;
    }

    public List<Mapcell> getMapcell(long userId, int maparea_id, int mapinfo_no) {
        List<Mapcell> result = Mapcell.dao.find("select id,passed from Mapcell where maparea_id=? and mapinfo_no=? and member_id=?", maparea_id, mapinfo_no, userId);
        return result;
    }
}
